﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace ParameterManagementSystem
{
    public class XmlFile
    {
        public int id;
        public string name;
        public string tag;
        public DateTime timeStamp;
        public string content;

        public void GenerateID(XmlFile[] filesArray)
        {
            if (filesArray.Count<XmlFile>() == 0)
            {
                id = 0;
                return;
            }
            int max_id = filesArray[0].id;
            foreach (XmlFile file in filesArray)
            {
                if (max_id < file.id)
                {
                    max_id = file.id;
                }
            }
            id = max_id + 1;
        }

        public bool CompareToParameterSearchValue(ParameterSearchValue param)
        {
            XmlDocument doc = new XmlDocument();
            doc.InnerXml = content;
            XmlNodeList xmlList = doc.DocumentElement.GetElementsByTagName("GROUP");
            foreach (XmlNode node in xmlList)
            {
                string groupName = ((XmlElement)node).GetAttribute("NAME");
                if (groupName == null || !groupName.Contains(param.group))
                {
                    continue;
                }
                XmlNodeList paramList = ((XmlElement)node).GetElementsByTagName("PARAM");
                foreach (XmlNode parameter in paramList)
                {
                    string parameterName = ((XmlElement)parameter).GetAttribute("NAME");
                    if (parameterName == null || !parameterName.Contains(param.parameterName))
                    {
                        continue;
                    }
                    string value = ((XmlElement)parameter).GetAttribute("VALUE");
                    if (value == null)
                    {
                        continue;
                    }
                    switch (param.valueType)
                    {
                        case (ParameterSearchValue.TYPE_INT):
                            int value_int;
                            try
                            {
                                value_int = Convert.ToInt32(value);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            if (param.range)
                            {
                                if (value_int > param.value1_int && value_int < param.value2_int)
                                {
                                    return true;
                                }
                            }
                            else if (value_int == param.value1_int)
                            {
                                return true;
                            }
                            break;
                        case (ParameterSearchValue.TYPE_DOUBLE):
                            double value_double;
                            try
                            {
                                value_double = Convert.ToDouble(value.Replace(".", ","));
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            if (param.range)
                            {
                                if (value_double > param.value1_double && value_double < param.value2_double)
                                {
                                    return true;
                                }
                            }
                            else if (value_double == param.value1_double)
                            {
                                return true;
                            }
                            break;
                        case (ParameterSearchValue.TYPE_BOOL):
                            bool value_bool;
                            try
                            {
                                value_bool = Convert.ToBoolean(value);
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                            if (value_bool == param.value_bool)
                            {
                                return true;
                            }
                            break;
                        case (ParameterSearchValue.TYPE_TEXT):
                            if (value == param.value_string)
                            {
                                return true;
                            }
                            break;
                    }
                }
            }
            return false;
        }

        public bool saveToFile(string file)
        {
            StreamWriter writer = new StreamWriter(file);
            writer.Write(content);
            writer.Close();
            return true;
        }

        public bool createFileTree(TreeView FileTreeView)
        {
            int group_counter = 0;
            int param_counter = 0;

            TreeNode tree_root = new TreeNode("PARAMETERS");
            tree_root.Tag = -1;
            FileTreeView.Nodes.Add(tree_root);

            XmlDocument doc = new XmlDocument();
            doc.InnerXml = content;
            XmlNodeList xmlList = doc.DocumentElement.GetElementsByTagName("GROUP");
            foreach (XmlNode node in xmlList)
            {
                string groupName = ((XmlElement)node).GetAttribute("NAME");
                TreeNode group_root = new TreeNode("GROUP NAME=" + groupName);
                group_root.Tag = group_counter;
                tree_root.Nodes.Add(group_root);

                XmlNodeList paramList = ((XmlElement)node).GetElementsByTagName("PARAM");

                param_counter = 0;
                foreach (XmlNode parameter in paramList)
                {
                    string parameterName = ((XmlElement)parameter).GetAttribute("NAME");
                    TreeNode param_node = new TreeNode("PARAM NAME=" + parameterName);
                    param_node.Tag = new int[] { group_counter, param_counter++ };

                    group_root.Nodes.Add(param_node);
                }
                group_counter++;
            }
            return true;
        }

        public XmlNode get_param(int group_id, int param_id)
        {
            XmlNode searched_group;
            XmlNode searched_param;

            XmlDocument doc = new XmlDocument();
            doc.InnerXml = content;
            XmlNodeList xmlList = doc.DocumentElement.GetElementsByTagName("GROUP");
            searched_group = xmlList[group_id];

            XmlNodeList paramList = ((XmlElement)searched_group).GetElementsByTagName("PARAM");
            searched_param = paramList[param_id];

            return searched_param;
        }

        public XmlNode get_group(int group_id)
        {
            XmlNode searched_group;

            XmlDocument doc = new XmlDocument();
            doc.InnerXml = content;
            XmlNodeList xmlList = doc.DocumentElement.GetElementsByTagName("GROUP");
            searched_group = xmlList[group_id];

            return searched_group;
        }

        public bool set_param(int group_id, int param_id, string new_value)
        {
            XmlNode searched_group;
            XmlNode searched_param;

            XmlDocument doc = new XmlDocument();
            doc.InnerXml = content;
            XmlNodeList xmlList = doc.DocumentElement.GetElementsByTagName("GROUP");
            searched_group = xmlList[group_id];

            XmlNodeList paramList = ((XmlElement)searched_group).GetElementsByTagName("PARAM");
            searched_param = paramList[param_id];

            ((XmlElement)searched_param).SetAttribute("VALUE", new_value);
            content = doc.InnerXml;

            return true;
        }

    }
}
